<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ctrl_produk extends CI_Controller {

	var $data;

	public function __construct(){
		parent::__construct();

		$this->output->enable_profiler(false);

		$this->load->model('model_produk');
    $this->load->model('model_kategori');

    $this->data['getproduk'] = $this->model_produk->getproduk();
    $this->data['getkategori'] = $this->model_kategori->getAllkategori();


		$this->load->library('upload');

	}

	public function tambahproduk(){

		$this->load->model('model_produk');
		$this->load->model('model_kategori');

		$this->data['getproduk'] = $this->model_produk->getproduk();
		$this->data['getkategori'] = $this->model_kategori->getAllkategori();

		$this->data['page']  = 'panel_produk_add';
		$this->data['title'] = 'Produk';
		$this->load->view('admin_panel', $this->data);
	}

	public function insertProdukDb()
	{
		$config['upload_path'] = './assets/img/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '2000';
		$config['max_height']  = '1024';
                
                $this->upload->initialize($config);
                if(!$this->upload->do_upload('gambar')){
                    $gambar="";
                }else{
                    $gambar=$this->upload->file_name;
                }
                
		//Function yang dipanggil ketika ingin memasukan produk ke dalam database
		$data = array(
					'id_kategori' => $this->input->post('pilihkategori'),
					'nama_produk' => $this->input->post('namaproduk'),
					'deskripsi' => $this->input->post('deskripsi'),
					'harga' => $this->input->post('harga'),
					'stok' => $this->input->post('stok'),
					'berat' => $this->input->post('berat'),
					'tgl_masuk' => $this->input->post('tanggal'),
					'gambar' => $gambar
				);
		$this->model_produk->insertproduk($data); //passing variable $data ke products_model
		redirect('admin_panel/produk'); //redirect page ke halaman utama controller products
	}

	public function updateProduk($id){

		$this->load->model('model_kategori');
		$this->load->model('model_produk');

		$this->data['getkategori'] = $this->model_kategori->getAllkategori();
		
		$this->data['produk'] = $this->model_produk->getprodukid($id);

		$this->data['page']  = 'panel_produk_edit';
		$this->data['title'] = 'Edit Produk';
		$this->load->view('admin_panel', $this->data);
	}

	public function updateProdukDB(){
		$id=$this->input->post('id_produk'); 
		$data = array(
					'id_kategori' => $this->input->post('pilihkategori'),
					'nama_produk' => $this->input->post('namaproduk'),
					'deskripsi' => $this->input->post('deskripsi'),
					'harga' => $this->input->post('harga'),
					'stok' => $this->input->post('stok'),
					'berat' => $this->input->post('berat'),
					'tgl_masuk' => $this->input->post('tanggal'),
					'gambar' => $this->input->post('gambar')
				);
		$this->model_produk->updateProduk($id,$data);
		redirect('admin_panel/produk');
	}

	public function deleteProdukDB($id_produk) 
	{
		$this->model_produk->deleteProdukDB($id_produk);		
		redirect('admin_panel/produk');
	}



}
